package com.it.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.travel.common.ConstEnum;
import com.it.travel.dao.OrderMapper;
import com.it.travel.dao.ProductMapper;
import com.it.travel.dao.ProductSkuMapper;
import com.it.travel.pojo.Order;
import com.it.travel.pojo.Product;
import com.it.travel.pojo.ProductSku;
import com.it.travel.service.OrderService;
import com.it.travel.vo.Params.OrderParams;
import com.it.travel.vo.Result;
import lombok.Data;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductSkuMapper productSkuMapper;

    @Transactional
    @Override
    public Result<?> update(OrderParams orderParams) {
        Integer productId = orderParams.getProductId();
        Integer count = orderParams.getCount();
        Integer userId = orderParams.getUserId();
        if (count < 0 || count > 10) {
            return Result.error(null, "数量不合法");
        }
        ProductSku productSku = productSkuMapper.selectOne(new QueryWrapper<ProductSku>().eq("product_id", orderParams.getProductId()));
        System.out.println("productSku = " + productSku);
        Product product = productMapper.selectById(productId);
        System.out.println("product = " + product);
        if (productSku.getStock() > 0) {
            int i = productSkuMapper.decreaseStock(productId, count, productSku.getVersion());
            if (i == 0) { // 更新失败 ，提示用户重试
                return Result.error(null, "请重试");
            }
            Order order = new Order();
            Double originalPrice = productSku.getOriginalPrice();
            Double sellPrice = productSku.getSellPrice();
            Double discounts = productSku.getDiscounts();
            order.setTotalAmount(originalPrice * count * discounts);
            order.setIsDeleted(0);
            order.setUserId(orderParams.getUserId());
            order.setOrderNum(UUID.randomUUID().toString().substring(0,15)); // 订单编号
            order.setOrderStatus(ConstEnum.PAY_STATUS.getNum());
            order.setPayType(ConstEnum.PAY_weixin.getNum());
            order.setProductName(productSku.getName());
            order.setActualAmount(sellPrice * count * discounts);
            order.setCreateTime(System.currentTimeMillis());
            order.setOrderRemark(orderParams.getOrderRemark());
            product.setSoldNum(count);
            int i1 = productMapper.updateById(product);// 更新销售额
            int insert = orderMapper.insert(order);
            if (i1 > 0 && insert > 0) {
                return Result.success(null, "更新成功");
            }
        }
        return Result.error(null, "更新失败");
    }
}
